package com.godGuang.consultation.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.godGuang.common.log.annotation.Log;
import com.godGuang.common.log.enums.BusinessType;
import com.godGuang.common.security.annotation.RequiresPermissions;
import com.godGuang.consultation.domain.Doctor;
import com.godGuang.consultation.service.IDoctorService;
import com.godGuang.common.core.web.controller.BaseController;
import com.godGuang.common.core.domain.Result;
import com.godGuang.common.core.utils.poi.ExcelUtil;
import com.godGuang.common.core.web.page.TableDataInfo;

/**
 * 问诊Controller
 *
 * @author godGuang
 * @date 2024-01-11
 */
@RestController
@RequestMapping("/consultation")
public class DoctorController extends BaseController
{
    @Autowired
    private IDoctorService doctorService;


    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    /**
     * 查询问诊列表
     */
    @RequiresPermissions("consultation:consultation:list")
    @GetMapping("/list")
    public Result<TableDataInfo<Doctor>>  list(Doctor doctor)
    {
        if (redisTemplate.hasKey("doctorList")){
            List<Object> list = redisTemplate.opsForList().range("doctorList", 0, -1);
            ArrayList<Doctor> doctors = new ArrayList<>();
            for (Object str : list) {
                String strList = (String) str;
                Doctor parsedObject = JSONObject.parseObject(strList, Doctor.class);
                doctors.add(parsedObject);
            }
            return getDataTable(doctors);
        }
        startPage();
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        for (Doctor doctor1 : list) {
            redisTemplate.opsForList().rightPush("doctorList",JSONObject.toJSONString(doctor1));
        }
        return getDataTable(list);
    }



    /**
     * 导出问诊列表
     */
    @RequiresPermissions("consultation:consultation:export")
    @Log(title = "问诊", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Doctor doctor)
    {
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        ExcelUtil<Doctor> util = new ExcelUtil<Doctor>(Doctor.class);
        util.exportExcel(response, list, "问诊数据");
    }

    /**
     * 获取问诊详细信息
     */
    @RequiresPermissions("consultation:consultation:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return success(doctorService.selectDoctorById(id));
    }

    /**
     * 新增问诊
     */
    @RequiresPermissions("consultation:consultation:add")
    @Log(title = "问诊", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody Doctor doctor)
    {
        return toAjax(doctorService.insertDoctor(doctor));
    }

    /**
     * 修改问诊
     */
    @RequiresPermissions("consultation:consultation:edit")
    @Log(title = "问诊", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody Doctor doctor)
    {
        return toAjax(doctorService.updateDoctor(doctor));
    }

    /**
     * 删除问诊
     */
    @RequiresPermissions("consultation:consultation:remove")
    @Log(title = "问诊", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return toAjax(doctorService.deleteDoctorByIds(ids));
    }
}
